//
//  AnchorViewController.swift
//  KyTV
//
//  Created by fangyukui on 2017/11/26.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit

fileprivate let cellId = "HomeViewCellId"

class AnchorViewController: UIViewController {
    
    fileprivate var homeVm = HomeViewModel()
    
    fileprivate lazy var collectionView : UICollectionView =  {()->UICollectionView  in
        let layout =   KyWaterfallLayout()
        layout.itemSize = CGSize(width: 80, height: 80)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.dataSource = self
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        let vc =  UICollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height), collectionViewLayout: layout)
        vc.delegate = self
        vc.dataSource = self
        return vc
        
    }()
    
    var themeM: ThemeModel? {
        didSet{
            self.homeVm.loadHomeData(type: themeM!, index: 1) {
                self.collectionView.reloadData()
            }
        }
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    

}

extension AnchorViewController{
    fileprivate func setupUI(){
        view.backgroundColor = .black
        collectionView.register(UINib(nibName: "HomeViewCell", bundle: nil), forCellWithReuseIdentifier: cellId)
        view.addSubview(collectionView)
        
    }
}

extension AnchorViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return homeVm.anchorModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomeViewCell
        let anchorM  =  homeVm.anchorModels[indexPath.item]
        print(anchorM.name);
        
        item.anchorModel = anchorM
        
        return item
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = RoomViewController(nibName: "RoomViewController", bundle: nil)
        
        navigationController?.pushViewController(vc, animated: true)
        
        
    }
    
}

extension AnchorViewController:KyWaterfallLayoutDataSource{
    func numberOfColsInWaterfallLayout(layout: KyWaterfallLayout) -> Int {
        return 2
    }
    
    func waterfallLayout(layout: KyWaterfallLayout, hightForItem indexPath: IndexPath) -> CGFloat {
        return  CGFloat(arc4random_uniform(150) + 100)
    }
    
    
}

